//
//  MyCommentsController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/26.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "MyCommentsController.h"

@interface MyCommentsController ()<SGPageTitleViewDelegate,UITableViewDelegate,UITableViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)SGPageTitleView*titleView;
/**<#属性#>*/
@property (nonatomic,strong)SGPageTitleViewConfigure*configure;

/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,assign)NSInteger selectedIndex;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

@implementation MyCommentsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的评论";
    [self createUI];
}
-(void)createUI{
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.listView];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.mas_equalTo(self.titleView.mas_bottom);
    }];
    
}
#pragma mark ----SGPageTitleViewDelegate

/**
 *  联动 pageContent 的方法
 *
 *  @param pageTitleView      SGPageTitleView
 *  @param selectedIndex      选中按钮的下标
 */
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex{
    self.selectedIndex = selectedIndex;
    [self.listView reloadData];
}
#pragma mark UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCommentsOneCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyCommentsOneCell class])];
    MyCommentsTwoCell * twoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyCommentsTwoCell class])];
    MyCommentsModel *model =  self.dataArray[indexPath.row];
    
    if (self.selectedIndex == 0) {
        cell.replyMe = @"回复我:";
        twoCell.replyMe = @"回复我:";
    }else{
        cell.replyMe = @"";
        twoCell.replyMe = @"";
    }
    if (model.type == MyCommentsText) {
        twoCell.model = model;
        return twoCell;
    }
    cell.model = model;
    return cell;
}
-(SGPageTitleViewConfigure*)configure{
    if(!_configure){
        _configure=[SGPageTitleViewConfigure pageTitleViewConfigure];
//        _configure.titleAdditionalWidth = 30;
        _configure.titleGradientEffect = YES;
        _configure.titleSelectedColor = UIColorFromRGB(0x009966);
        _configure.indicatorColor = UIColorFromRGB(0x009966);
        
    }
    return _configure;
}

-(SGPageTitleView*)titleView{
    if(!_titleView){
        NSArray *titleArr = @[@"回复我的", @"我发出的"];
        _titleView=[SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, KScreenWidth, 44.0) delegate:self titleNames:titleArr configure:self.configure];
        
    }
    return _titleView;
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[MyCommentsOneCell class] forCellReuseIdentifier:NSStringFromClass([MyCommentsOneCell class])];
        [_listView registerClass:[MyCommentsTwoCell class] forCellReuseIdentifier:NSStringFromClass([MyCommentsTwoCell class])];

        _listView.tableFooterView = UIView.new;
        [_listView setSeparatorInset:UIEdgeInsetsMake(0, 0, KScreenWidth, 0.5)];
        _listView.separatorColor = SeparatorColor;
    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyComments" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MyCommentsModel *model = [MyCommentsModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];
    }
    return _dataArray;
}
@end


@interface MyCommentsOneCell ()


/**用户头像*/
@property (nonatomic,strong)UIImageView*iconImage;
/**用户名称*/
@property (nonatomic,strong)UILabel*userName;
/**动态时间*/
@property (nonatomic,strong)UILabel*dynamicData;
/** '回复我' 文字*/
@property (nonatomic,strong)UILabel*replyMeLabel;
/**被回复二级内容 是图片(视频)+文字 样式*/
@property (nonatomic,strong)UIView*imageAndTextView;
/**回复内容*/
@property (nonatomic,strong)UILabel * contenText;
/**<#属性#>*/
@property (nonatomic,strong)UIImageView * leftImage;
/**二级用户姓名*/
@property (nonatomic,strong)UILabel*twoUserName;
/**二级回复内容*/
@property (nonatomic,strong)UILabel*twoContenText;

/**播放按钮*/
@property (nonatomic,strong)UIButton*payBtn;
@end

@implementation MyCommentsOneCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setReplyMe:(NSString *)replyMe{
    _replyMe = replyMe;
    self.replyMeLabel.text = replyMe;
}
-(void)setModel:(MyCommentsModel *)model{
    _model = model;
    self.userName.text = model.userName;
    self.iconImage.image = IMAGE_NAMED(model.userImage);
    self.contenText.text = model.contex;
    
    if (model.type == MyCommentsTextAndVideo) {
        [self.leftImage imageViewWithUrl:model.leftImage placeholderImage:@""];
        self.payBtn.hidden = NO;
    }else{
        self.payBtn.hidden = YES;
        self.leftImage.image = IMAGE_NAMED(model.leftImage);
    }
    self.twoUserName.text = model.twoUserName;
    self.twoContenText.text = model.twoContex;
    
}
-(void)createUI{
    
    self.contenText = UILabel.new;
    self.contenText.textColor = UIColorFromRGB(0x273323);
    self.contenText.font = SYSTEMFONT(16);
    self.contenText.numberOfLines = 0;
    self.contenText.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:@"好好吃哦 这个烤鸡怎么做的呀，告诉我一下这地方更多丰富的官方回复结核啊…" titleColor:UIColorFromRGB(0x273323) titleFont:PingFangSC_Regular(16)];
    self.imageAndTextView = [self createLeftImageAndTextView];
    
    
    [self addSubview:self.iconImage];
    [self addSubview:self.userName];
    [self addSubview:self.dynamicData];
    [self addSubview:self.replyMeLabel];
    [self addSubview:self.contenText];
    [self addSubview:self.imageAndTextView];


    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.width.height.mas_equalTo(40);
    }];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImage.mas_right).offset(12);
        make.top.mas_equalTo(self.iconImage);
    }];
    [self.dynamicData mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.iconImage);
    }];
    [self.replyMeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(self.iconImage.mas_right).offset(12);
    make.top.mas_equalTo(self.userName.mas_bottom).offset(8);
    }];
    [self.contenText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userName);
    make.top.mas_equalTo(self.replyMeLabel.mas_bottom).offset(4);
        make.right.mas_equalTo(-16);
    }];
    
    [self.imageAndTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contenText);
        make.top.mas_equalTo(self.contenText.mas_bottom).offset(16);
        make.bottom.mas_equalTo(-16);
    }];
}

#pragma mark --懒加载
-(UIImageView*)iconImage{
    if(!_iconImage){
        _iconImage=[UIImageView new];
        _iconImage.layer.cornerRadius = 20;
        _iconImage.image = IMAGE_NAMED(@"userIconDefut");
        _iconImage.userInteractionEnabled = YES;
        _iconImage.layer.masksToBounds=YES;
//        [_iconImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushUserContenPage)]];
    }
    return _iconImage;
}
-(UILabel*)userName{
    if(!_userName){
        _userName=[UILabel new];
        _userName.font = PingFangSC_Medium(16);
        _userName.textColor = UIColorFromRGB(0x273323);
        _userName.text = @"我是大鲨鱼";
        _userName.userInteractionEnabled = YES;
//        [_userName addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushUserContenPage)]];

    }
    return _userName;
}
-(UILabel*)dynamicData{
    if(!_dynamicData){
        _dynamicData=[UILabel new];
        _dynamicData.font = PingFangSC_Regular(12);
        _dynamicData.textColor = UIColorFromRGB(0x959E97);
        _dynamicData.text = @"昨天 14:15";

    }
    return _dynamicData;
}

-(UILabel*)replyMeLabel{
    if(!_replyMeLabel){
        _replyMeLabel=[UILabel new];
        _replyMeLabel.font = PingFangSC_Regular(14);
        _replyMeLabel.textColor = UIColorFromRGB(0x959E97);
        _replyMeLabel.text = self.replyMe;
    }
    return _replyMeLabel;
}
-(UIButton*)payBtn{
    if(!_payBtn){
        _payBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_payBtn setImage:IMAGE_NAMED(@"video_play") forState:UIControlStateNormal];
        _payBtn.hidden = YES;
    }
    return _payBtn;
}
-(UIView *)createLeftImageAndTextView{
    UIView * view = UIView.new;
    view.backgroundColor = UIColorFromRGB(0xF5F7F5);
    
    self.leftImage = UIImageView.new;
    self.leftImage.image = IMAGE_NAMED(@"dynamicDefut");
    
    self.twoUserName = UILabel.new;
    self.twoUserName.textColor = UIColorFromRGB(0x273323);
    self.twoUserName.font = PingFangSC_Regular(14);
    self.twoUserName.text = @"@我是大鲨鱼";
    
    self.twoContenText = UILabel.new;
    self.twoContenText.textColor = UIColorFromRGB(0x454F44);
    self.twoContenText.font = SYSTEMFONT(12);
    self.twoContenText.numberOfLines = 2;
    self.twoContenText.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:@"做烤鸡的时候，千万别直接放进烤箱，多加一步，鸡肉更加入味" titleColor:UIColorFromRGB(0x454F44) titleFont:PingFangSC_Regular(12)];


    [view addSubview:self.leftImage];
    [view addSubview:self.twoUserName];
    [view addSubview:self.twoContenText];
    
    [self.leftImage addSubview:self.payBtn];
    
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.height.mas_equalTo(96);
    }];
    [self.twoUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftImage.mas_right).offset(16);
        make.top.mas_equalTo(self.leftImage.mas_top).offset(16);
        make.right.mas_equalTo(0);
    }];
    [self.twoContenText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.twoUserName);
        make.top.mas_equalTo(self.twoUserName.mas_bottom).offset(4);
//        make.bottom.mas_equalTo(self.leftImage);
    }];
    
    [self.payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    return view;
}

@end


@interface MyCommentsTwoCell ()

/**用户头像*/
@property (nonatomic,strong)UIImageView*iconImage;
/**用户名称*/
@property (nonatomic,strong)UILabel*userName;
/**动态时间*/
@property (nonatomic,strong)UILabel*dynamicData;
/** '回复我' 文字*/
@property (nonatomic,strong)UILabel*replyMeLabel;
/**被回复二级内容 是纯文字 样式*/
@property (nonatomic,strong)UIView*textView;
/**回复内容*/
@property (nonatomic,strong)UILabel * contenText;
/**二级用户姓名*/
@property (nonatomic,strong)UILabel*twoUserName2;
/**二级回复内容*/
@property (nonatomic,strong)UILabel*twoContenText2;
@end

@implementation MyCommentsTwoCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setReplyMe:(NSString *)replyMe{
    _replyMe = replyMe;
    self.replyMeLabel.text = replyMe;
}
-(void)setModel:(MyCommentsModel *)model{
    _model = model;
    self.userName.text = model.userName;
    self.iconImage.image = IMAGE_NAMED(model.userImage);
    self.contenText.text = model.contex;
    self.twoUserName2.text = model.twoUserName;
    self.twoContenText2.text = model.twoContex;

}
-(void)createUI{
    
    self.contenText = UILabel.new;
    self.contenText.textColor = UIColorFromRGB(0x273323);
    self.contenText.font = SYSTEMFONT(16);
    self.contenText.numberOfLines = 0;
    self.contenText.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:@"好好吃哦 这个烤鸡怎么做的呀，告诉我一下这地方更多丰富的官方回复结核啊…" titleColor:UIColorFromRGB(0x273323) titleFont:PingFangSC_Regular(16)];

    self.textView = [self createTextView];
    
    [self addSubview:self.iconImage];
    [self addSubview:self.userName];
    [self addSubview:self.dynamicData];
    [self addSubview:self.replyMeLabel];
    [self addSubview:self.contenText];
    [self addSubview:self.textView];


    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.width.height.mas_equalTo(40);
    }];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImage.mas_right).offset(12);
        make.top.mas_equalTo(self.iconImage);
    }];
    [self.dynamicData mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.iconImage);
    }];
    [self.replyMeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(self.iconImage.mas_right).offset(12);
    make.top.mas_equalTo(self.userName.mas_bottom).offset(8);
    }];
    [self.contenText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userName);
    make.top.mas_equalTo(self.replyMeLabel.mas_bottom).offset(4);
        make.right.mas_equalTo(-16);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contenText);
        make.top.mas_equalTo(self.contenText.mas_bottom).offset(16);
        make.bottom.mas_equalTo(-16);
    }];
}

#pragma mark --懒加载
-(UIImageView*)iconImage{
    if(!_iconImage){
        _iconImage=[UIImageView new];
        _iconImage.layer.cornerRadius = 20;
        _iconImage.image = IMAGE_NAMED(@"userIconDefut");
        _iconImage.userInteractionEnabled = YES;
        _iconImage.layer.masksToBounds=YES;
//        [_iconImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushUserContenPage)]];
    }
    return _iconImage;
}
-(UILabel*)userName{
    if(!_userName){
        _userName=[UILabel new];
        _userName.font = PingFangSC_Medium(16);
        _userName.textColor = UIColorFromRGB(0x273323);
        _userName.text = @"我是大鲨鱼";
        _userName.userInteractionEnabled = YES;
//        [_userName addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushUserContenPage)]];

    }
    return _userName;
}
-(UILabel*)dynamicData{
    if(!_dynamicData){
        _dynamicData=[UILabel new];
        _dynamicData.font = PingFangSC_Regular(12);
        _dynamicData.textColor = UIColorFromRGB(0x959E97);
        _dynamicData.text = @"昨天 14:15";

    }
    return _dynamicData;
}

-(UILabel*)replyMeLabel{
    if(!_replyMeLabel){
        _replyMeLabel=[UILabel new];
        _replyMeLabel.font = PingFangSC_Regular(14);
        _replyMeLabel.textColor = UIColorFromRGB(0x959E97);
        _replyMeLabel.text = self.replyMe;
    }
    return _replyMeLabel;
}
-(UIView *)createTextView{
    UIView * view = UIView.new;
    view.backgroundColor = UIColorFromRGB(0xF5F7F5);
    
    self.twoUserName2 = UILabel.new;
    self.twoUserName2.textColor = UIColorFromRGB(0x273323);
    self.twoUserName2.font = SYSTEMFONT(14);
    self.twoUserName2.text = @"@我是大鲨鱼";
    
    self.twoContenText2 = UILabel.new;
    self.twoContenText2.textColor = UIColorFromRGB(0x454F44);
    self.twoContenText2.font = SYSTEMFONT(12);
    self.twoContenText2.numberOfLines = 2;
    self.twoContenText2.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:@"做烤鸡的时候，千万别直接放进烤箱，多加一步，鸡肉更加入味" titleColor:UIColorFromRGB(0x454F44) titleFont:PingFangSC_Regular(12)];


    [view addSubview:self.twoUserName2];
    [view addSubview:self.twoContenText2];
    
    
    [self.twoUserName2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    [self.twoContenText2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.twoUserName2);
        make.top.mas_equalTo(self.twoUserName2.mas_bottom).offset(4);
        make.bottom.mas_equalTo(-16);
    }];
    
    
    return view;
}

@end
